package cn.fl.framework.handler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.cmm.base.framework.utils.StringUtils;

public class ResponseFilter implements Filter {

	public static List<String> keyList = new ArrayList<>();
	public static Map<String, String> convertKeyMap = new HashMap<>();
	static {
		keyList.add("吾斯曼");
		keyList.add("伊力哈木");
		keyList.add("信用卡套现");

		convertKeyMap.put("吾斯曼", "吾-斯曼");
		convertKeyMap.put("伊力哈木", "伊力-哈木");
		convertKeyMap.put("信用卡套现", "信用卡-套现");

	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
        //由于web.xml中设置Filter过滤全部请求，可以排除不需要过滤的url
        String reqURI = req.getRequestURI();
        if(reqURI.endsWith(".html") 
        		|| reqURI.endsWith(".htm")
        		|| reqURI.endsWith(".js")
        		|| reqURI.endsWith(".css")
        		|| reqURI.endsWith(".jpg")
        		|| reqURI.endsWith(".jpeg")
        		|| reqURI.endsWith(".png")
        		|| reqURI.endsWith(".png")
        		|| reqURI.endsWith(".ico")
        		|| reqURI.endsWith("exportRentNotice.do")
        		|| reqURI.endsWith("exportFirstPayAmt.do")
        		|| reqURI.endsWith("exportPrjPrdPaySch.do")
        		|| reqURI.endsWith("zipDownloadById.do")
        		) {
        	chain.doFilter(request, response);
        	return;
        }
		ResponseWrapper wrapperResponse = new ResponseWrapper((HttpServletResponse) response);// 转换成代理类
		// 这里只拦截返回，直接让请求过去，如果在请求前有处理，可以在这里处理
		chain.doFilter(request, wrapperResponse);
		//放过这种下载数据的操作
		String contentType = wrapperResponse.getContentType();
		if(StringUtils.isNotEmpty(contentType)){
			if(contentType.contains("application/x-msdownload")
				|| contentType.contains("application/pdf")
				|| contentType.contains("application/zip")
				|| contentType.contains("application/x-xls")
				|| contentType.contains("application/vnd.ms-excel")
					){
				chain.doFilter(request, response);
				return;
			}
		}
		byte[] content = wrapperResponse.getContent();// 获取返回值
		// 判断是否有值
		if (null != content && content.length > 0) {
			String str = new String(content, "UTF-8");
			try {
				if (StringUtils.isNotEmpty(str)) {
					for (String key : keyList) {
						if (str.contains(key)) {
							str = str.replace(key, convertKeyMap.get(key));
						}
					}
				}
				response.getWriter().append(str);
			} catch (Exception e) {
				e.printStackTrace();
				chain.doFilter(request, wrapperResponse);
			}
		}
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

}
